home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perlop.Z / perlop
Encoding:
Text File  |  1998-10-28  |  88.7 KB  |  2,509 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perlop - Perl    operators and precedence
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.       Perl operators have the following associativity and
  13.       precedence, listed from highest precedence to    lowest.     Note
  14.       that all operators borrowed from C keep the same precedence
  15.       relationship with each other,    even where C's precedence is
  16.       slightly screwy.  (This makes    learning Perl easier for C
  17.       folks.)  With    very few exceptions, these all operate on
  18.       scalar values    only, not array    values.
  19.  
  20.           left      terms    and list operators (leftward)
  21.           left      ->
  22.           nonassoc      ++ --
  23.           right      **
  24.           right      ! ~ \    and unary + and    -
  25.           left      =~ !~
  26.           left      * / %    x
  27.           left      + - .
  28.           left      << >>
  29.           nonassoc      named    unary operators
  30.           nonassoc      < > <= >= lt gt le ge
  31.           nonassoc      == !=    <=> eq ne cmp
  32.           left      &
  33.           left      | ^
  34.           left      &&
  35.           left      ||
  36.           nonassoc      ..  ...
  37.           right      ?:
  38.           right      = += -= *= etc.
  39.           left      , =>
  40.           nonassoc      list operators (rightward)
  41.           right      not
  42.           left      and
  43.           left      or xor
  44.  
  45.       In the following sections, these operators are covered in
  46.       precedence order.
  47.  
  48.       Many operators can be    overloaded for objects.     See the
  49.       _o_v_e_r_l_o_a_d manpage.
  50.  
  51.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  52.       TTTTeeeerrrrmmmmssss    aaaannnndddd LLLLiiiisssstttt OOOOppppeeeerrrraaaattttoooorrrrssss ((((LLLLeeeeffffttttwwwwaaaarrrrdddd))))
  53.  
  54.       A TERM has the highest precedence in Perl.  They includes
  55.       variables, quote and quote-like operators, any expression in
  56.       parentheses, and any function    whose arguments    are
  57.       parenthesized.  Actually, there aren't really    functions in
  58.       this sense, just list    operators and unary operators behaving
  59.       as functions because you put parentheses around the
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  71.  
  72.  
  73.  
  74.       arguments.  These are    all documented in the _p_e_r_l_f_u_n_c
  75.       manpage.
  76.  
  77.       If any list operator (_p_r_i_n_t(), etc.) or any unary operator
  78.       (_c_h_d_i_r(), etc.)  is followed by a left parenthesis as    the
  79.       next token, the operator and arguments within    parentheses
  80.       are taken to be of highest precedence, just like a normal
  81.       function call.
  82.  
  83.       In the absence of parentheses, the precedence    of list
  84.       operators such as print, sort, or chmod is either very high
  85.       or very low depending    on whether you are looking at the left
  86.       side or the right side of the    operator.  For example,    in
  87.  
  88.           @ary = (1, 3, sort 4, 2);
  89.           print @ary;      # prints 1324
  90.  
  91.       the commas on    the right of the sort are evaluated before the
  92.       sort,    but the    commas on the left are evaluated after.     In
  93.       other    words, list operators tend to gobble up    all the
  94.       arguments that follow    them, and then act like    a simple TERM
  95.       with regard to the preceding expression.  Note that you have
  96.       to be    careful    with parentheses:
  97.  
  98.           #    These evaluate exit before doing the print:
  99.           print($foo, exit);  # Obviously not what you want.
  100.           print $foo, exit;      # Nor    is this.
  101.  
  102.           #    These do the print before evaluating exit:
  103.           (print $foo), exit; # This is what you want.
  104.           print($foo), exit;  # Or this.
  105.           print ($foo), exit; # Or even this.
  106.  
  107.       Also note that
  108.  
  109.           print ($foo & 255) + 1, "\n";
  110.  
  111.       probably doesn't do what you expect at first glance.    See
  112.       the section on _N_a_m_e_d _U_n_a_r_y _O_p_e_r_a_t_o_r_s for more    discussion of
  113.       this.
  114.  
  115.       Also parsed as terms are the do {} and eval {} constructs,
  116.       as well as subroutine    and method calls, and the anonymous
  117.       constructors [] and {}.
  118.  
  119.       See also the section on _Q_u_o_t_e    _a_n_d _Q_u_o_t_e-_l_i_k_e _O_p_e_r_a_t_o_r_s
  120.       toward the end of this section, as well as the section on
  121.       _I/_O _O_p_e_r_a_t_o_r_s.
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  137.  
  138.  
  139.  
  140.       TTTThhhheeee AAAArrrrrrrroooowwww OOOOppppeeeerrrraaaattttoooorrrr
  141.  
  142.       Just as in C and C++,    "->" is    an infix dereference operator.
  143.       If the right side is either a    [...] or {...} subscript, then
  144.       the left side    must be    either a hard or symbolic reference to
  145.       an array or hash (or a location capable of holding a hard
  146.       reference, if    it's an    lvalue (assignable)).  See the _p_e_r_l_r_e_f
  147.       manpage.
  148.  
  149.       Otherwise, the right side is a method    name or    a simple
  150.       scalar variable containing the method    name, and the left
  151.       side must either be an object    (a blessed reference) or a
  152.       class    name (that is, a package name).     See the _p_e_r_l_o_b_j
  153.       manpage.
  154.  
  155.       AAAAuuuuttttoooo----iiiinnnnccccrrrreeeemmmmeeeennnntttt aaaannnndddd AAAAuuuuttttoooo----ddddeeeeccccrrrreeeemmmmeeeennnntttt
  156.  
  157.       "++" and "--"    work as    in C.  That is,    if placed before a
  158.       variable, they increment or decrement    the variable before
  159.       returning the    value, and if placed after, increment or
  160.       decrement the    variable after returning the value.
  161.  
  162.       The auto-increment operator has a little extra builtin magic
  163.       to it.  If you increment a variable that is numeric, or that
  164.       has ever been    used in    a numeric context, you get a normal
  165.       increment.  If, however, the variable    has been used in only
  166.       string contexts since    it was set, and    has a value that is
  167.       not the empty    string and matches the pattern /^[a-zA-Z]*[0-
  168.       9]*$/, the increment is done as a string, preserving each
  169.       character within its range, with carry:
  170.  
  171.           print ++($foo = '99');      # prints '100'
  172.           print ++($foo = 'a0');      # prints 'a1'
  173.           print ++($foo = 'Az');      # prints 'Ba'
  174.           print ++($foo = 'zz');      # prints 'aaa'
  175.  
  176.       The auto-decrement operator is not magical.
  177.  
  178.       EEEExxxxppppoooonnnneeeennnnttttiiiiaaaattttiiiioooonnnn
  179.  
  180.       Binary "**" is the exponentiation operator.  Note that it
  181.       binds    even more tightly than unary minus, so -2**4 is
  182.       -(2**4), not (-2)**4.    (This is implemented using C's _p_o_w(3)
  183.       function, which actually works on doubles internally.)
  184.  
  185.       SSSSyyyymmmmbbbboooolllliiiicccc UUUUnnnnaaaarrrryyyy OOOOppppeeeerrrraaaattttoooorrrrssss
  186.  
  187.       Unary    "!" performs logical negation, i.e., "not".  See also
  188.       not for a lower precedence version of    this.
  189.  
  190.       Unary    "-" performs arithmetic    negation if the    operand    is
  191.       numeric.  If the operand is an identifier, a string
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  203.  
  204.  
  205.  
  206.       consisting of    a minus    sign concatenated with the identifier
  207.       is returned.    Otherwise, if the string starts    with a plus or
  208.       minus, a string starting with    the opposite sign is returned.
  209.       One effect of    these rules is that -bareword is equivalent to
  210.       "-bareword".
  211.  
  212.       Unary    "~" performs bitwise negation, i.e., 1's complement.
  213.       For example, 0666 &~ 027 is 0640.  (See also the section on
  214.       _I_n_t_e_g_e_r _A_r_i_t_h_m_e_t_i_c and the section on    _B_i_t_w_i_s_e    _S_t_r_i_n_g
  215.       _O_p_e_r_a_t_o_r_s.)
  216.  
  217.       Unary    "+" has    no effect whatsoever, even on strings.    It is
  218.       useful syntactically for separating a    function name from a
  219.       parenthesized    expression that    would otherwise    be interpreted
  220.       as the complete list of function arguments.  (See examples
  221.       above    under the section on _T_e_r_m_s _a_n_d _L_i_s_t _O_p_e_r_a_t_o_r_s
  222.       (_L_e_f_t_w_a_r_d).)
  223.  
  224.       Unary    "\" creates a reference    to whatever follows it.     See
  225.       the _p_e_r_l_r_e_f manpage.    Do not confuse this behavior with the
  226.       behavior of backslash    within a string, although both forms
  227.       do convey the    notion of protecting the next thing from
  228.       interpretation.
  229.  
  230.       BBBBiiiinnnnddddiiiinnnngggg OOOOppppeeeerrrraaaattttoooorrrrssss
  231.  
  232.       Binary "=~" binds a scalar expression    to a pattern match.
  233.       Certain operations search or modify the string $_ by
  234.       default.  This operator makes    that kind of operation work on
  235.       some other string.  The right    argument is a search pattern,
  236.       substitution,    or transliteration.  The left argument is what
  237.       is supposed to be searched, substituted, or transliterated
  238.       instead of the default $_.  The return value indicates the
  239.       success of the operation.  (If the right argument is an
  240.       expression rather than a search pattern, substitution, or
  241.       transliteration, it is interpreted as    a search pattern at
  242.       run time.  This can be is less efficient than    an explicit
  243.       search, because the pattern must be compiled every time the
  244.       expression is    evaluated.
  245.  
  246.       Binary "!~" is just like "=~"    except the return value    is
  247.       negated in the logical sense.
  248.  
  249.       MMMMuuuullllttttiiiipppplllliiiiccccaaaattttiiiivvvveeee OOOOppppeeeerrrraaaattttoooorrrrssss
  250.  
  251.       Binary "*" multiplies    two numbers.
  252.  
  253.       Binary "/" divides two numbers.
  254.  
  255.       Binary "%" computes the modulus of two numbers.  Given
  256.       integer operands $a and $b: If $b is positive, then $a % $b
  257.       is $a    minus the largest multiple of $b that is not greater
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  269.  
  270.  
  271.  
  272.       than $a.  If $b is negative, then $a % $b is $a minus    the
  273.       smallest multiple of $b that is not less than    $a (i.e. the
  274.       result will be less than or equal to zero). Note than    when
  275.       use integer is in scope, "%" give you    direct access to the
  276.       modulus operator as implemented by your C compiler.  This
  277.       operator is not as well defined for negative operands, but
  278.       it will execute faster.
  279.  
  280.       Binary "x" is    the repetition operator.  In scalar context,
  281.       it returns a string consisting of the    left operand repeated
  282.       the number of    times specified    by the right operand.  In list
  283.       context, if the left operand is a list in parentheses, it
  284.       repeats the list.
  285.  
  286.           print '-'    x 80;          # print row of dashes
  287.  
  288.           print "\t" x ($tab/8), ' ' x ($tab%8);      # tab    over
  289.  
  290.           @ones = (1) x 80;          # a list of 80 1's
  291.           @ones = (5) x @ones;      # set    all elements to    5
  292.  
  293.  
  294.       AAAAddddddddiiiittttiiiivvvveeee OOOOppppeeeerrrraaaattttoooorrrrssss
  295.  
  296.       Binary "+" returns the sum of    two numbers.
  297.  
  298.       Binary "-" returns the difference of two numbers.
  299.  
  300.       Binary "." concatenates two strings.
  301.  
  302.       SSSShhhhiiiifffftttt    OOOOppppeeeerrrraaaattttoooorrrrssss
  303.  
  304.       Binary "<<" returns the value    of its left argument shifted
  305.       left by the number of    bits specified by the right argument.
  306.       Arguments should be integers.     (See also the section on
  307.       _I_n_t_e_g_e_r _A_r_i_t_h_m_e_t_i_c.)
  308.  
  309.       Binary ">>" returns the value    of its left argument shifted
  310.       right    by the number of bits specified    by the right argument.
  311.       Arguments should be integers.     (See also the section on
  312.       _I_n_t_e_g_e_r _A_r_i_t_h_m_e_t_i_c.)
  313.  
  314.       NNNNaaaammmmeeeedddd    UUUUnnnnaaaarrrryyyy OOOOppppeeeerrrraaaattttoooorrrrssss
  315.  
  316.       The various named unary operators are    treated    as functions
  317.       with one argument, with optional parentheses.     These include
  318.       the filetest operators, like -f, -M, etc.  See the _p_e_r_l_f_u_n_c
  319.       manpage.
  320.  
  321.       If any list operator (_p_r_i_n_t(), etc.) or any unary operator
  322.       (_c_h_d_i_r(), etc.)  is followed by a left parenthesis as    the
  323.       next token, the operator and arguments within    parentheses
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  335.  
  336.  
  337.  
  338.       are taken to be of highest precedence, just like a normal
  339.       function call.  Examples:
  340.  
  341.           chdir $foo    || die;      # (chdir $foo) || die
  342.           chdir($foo)   || die;      # (chdir $foo) || die
  343.           chdir ($foo)  || die;      # (chdir $foo) || die
  344.           chdir +($foo) || die;      # (chdir $foo) || die
  345.  
  346.       but, because * is higher precedence than ||:
  347.  
  348.           chdir $foo * 20;      # chdir ($foo    * 20)
  349.           chdir($foo) * 20;      # (chdir $foo) * 20
  350.           chdir ($foo) * 20;  # (chdir $foo) * 20
  351.           chdir +($foo) * 20; # chdir ($foo    * 20)
  352.  
  353.           rand 10 *    20;      # rand (10 * 20)
  354.           rand(10) * 20;      # (rand 10) *    20
  355.           rand (10)    * 20;      # (rand 10) *    20
  356.           rand +(10) * 20;      # rand (10 * 20)
  357.  
  358.       See also the section on _T_e_r_m_s    _a_n_d _L_i_s_t _O_p_e_r_a_t_o_r_s (_L_e_f_t_w_a_r_d).
  359.  
  360.       RRRReeeellllaaaattttiiiioooonnnnaaaallll OOOOppppeeeerrrraaaattttoooorrrrssss
  361.  
  362.       Binary "<" returns true if the left argument is numerically
  363.       less than the    right argument.
  364.  
  365.       Binary ">" returns true if the left argument is numerically
  366.       greater than the right argument.
  367.  
  368.       Binary "<=" returns true if the left argument    is numerically
  369.       less than or equal to    the right argument.
  370.  
  371.       Binary ">=" returns true if the left argument    is numerically
  372.       greater than or equal    to the right argument.
  373.  
  374.       Binary "lt" returns true if the left argument    is stringwise
  375.       less than the    right argument.
  376.  
  377.       Binary "gt" returns true if the left argument    is stringwise
  378.       greater than the right argument.
  379.  
  380.       Binary "le" returns true if the left argument    is stringwise
  381.       less than or equal to    the right argument.
  382.  
  383.       Binary "ge" returns true if the left argument    is stringwise
  384.       greater than or equal    to the right argument.
  385.  
  386.       EEEEqqqquuuuaaaalllliiiittttyyyy OOOOppppeeeerrrraaaattttoooorrrrssss
  387.  
  388.       Binary "==" returns true if the left argument    is numerically
  389.       equal    to the right argument.
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  401.  
  402.  
  403.  
  404.       Binary "!=" returns true if the left argument    is numerically
  405.       not equal to the right argument.
  406.  
  407.       Binary "<=>" returns -1, 0, or 1 depending on    whether    the
  408.       left argument    is numerically less than, equal    to, or greater
  409.       than the right argument.
  410.  
  411.       Binary "eq" returns true if the left argument    is stringwise
  412.       equal    to the right argument.
  413.  
  414.       Binary "ne" returns true if the left argument    is stringwise
  415.       not equal to the right argument.
  416.  
  417.       Binary "cmp" returns -1, 0, or 1 depending on    whether    the
  418.       left argument    is stringwise less than, equal to, or greater
  419.       than the right argument.
  420.  
  421.       "lt",    "le", "ge", "gt" and "cmp" use the collation (sort)
  422.       order    specified by the current locale    if use locale is in
  423.       effect.  See the _p_e_r_l_l_o_c_a_l_e manpage.
  424.  
  425.       BBBBiiiittttwwwwiiiisssseeee AAAAnnnndddd
  426.  
  427.       Binary "&" returns its operators ANDed together bit by bit.
  428.       (See also the    section    on _I_n_t_e_g_e_r _A_r_i_t_h_m_e_t_i_c and the section
  429.       on _B_i_t_w_i_s_e _S_t_r_i_n_g _O_p_e_r_a_t_o_r_s.)
  430.  
  431.       BBBBiiiittttwwwwiiiisssseeee OOOOrrrr aaaannnndddd EEEExxxxcccclllluuuussssiiiivvvveeee OOOOrrrr
  432.  
  433.       Binary "|" returns its operators ORed    together bit by    bit.
  434.       (See also the    section    on _I_n_t_e_g_e_r _A_r_i_t_h_m_e_t_i_c and the section
  435.       on _B_i_t_w_i_s_e _S_t_r_i_n_g _O_p_e_r_a_t_o_r_s.)
  436.  
  437.       Binary "^" returns its operators XORed together bit by bit.
  438.       (See also the    section    on _I_n_t_e_g_e_r _A_r_i_t_h_m_e_t_i_c and the section
  439.       on _B_i_t_w_i_s_e _S_t_r_i_n_g _O_p_e_r_a_t_o_r_s.)
  440.  
  441.       CCCC----ssssttttyyyylllleeee LLLLooooggggiiiiccccaaaallll AAAAnnnndddd
  442.  
  443.       Binary "&&" performs a short-circuit logical AND operation.
  444.       That is, if the left operand is false, the right operand is
  445.       not even evaluated.  Scalar or list context propagates down
  446.       to the right operand if it is    evaluated.
  447.  
  448.       CCCC----ssssttttyyyylllleeee LLLLooooggggiiiiccccaaaallll OOOOrrrr
  449.  
  450.       Binary "||" performs a short-circuit logical OR operation.
  451.       That is, if the left operand is true,    the right operand is
  452.       not even evaluated.  Scalar or list context propagates down
  453.       to the right operand if it is    evaluated.
  454.  
  455.       The || and &&    operators differ from C's in that, rather than
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  467.  
  468.  
  469.  
  470.       returning 0 or 1, they return    the last value evaluated.
  471.       Thus,    a reasonably portable way to find out the home
  472.       directory (assuming it's not "0") might be:
  473.  
  474.           $home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
  475.           (getpwuid($<))[7] || die "You're homeless!\n";
  476.  
  477.       In particular, this means that you shouldn't use this    for
  478.       selecting between two    aggregates for assignment:
  479.  
  480.           @a = @b || @c;          # this is wrong
  481.           @a = scalar(@b) || @c;      # really meant this
  482.           @a = @b ?    @b : @c;      # this works fine, though
  483.  
  484.       As more readable alternatives    to && and || when used for
  485.       control flow,    Perl provides and and or operators (see
  486.       below).  The short-circuit behavior is identical.  The
  487.       precedence of    "and" and "or" is much lower, however, so that
  488.       you can safely use them after    a list operator    without    the
  489.       need for parentheses:
  490.  
  491.           unlink "alpha", "beta", "gamma"
  492.               or gripe(), next LINE;
  493.  
  494.       With the C-style operators that would    have been written like
  495.       this:
  496.  
  497.           unlink("alpha", "beta", "gamma")
  498.               || (gripe(), next    LINE);
  499.  
  500.       Use "or" for assignment is unlikely to do what you want; see
  501.       below.
  502.  
  503.       RRRRaaaannnnggggeeee    OOOOppppeeeerrrraaaattttoooorrrrssss
  504.  
  505.       Binary ".." is the range operator, which is really two
  506.       different operators depending    on the context.     In list
  507.       context, it returns an array of values counting (by ones)
  508.       from the left    value to the right value.  This    is useful for
  509.       writing foreach (1..10) loops    and for    doing slice operations
  510.       on arrays.  In the current implementation, no    temporary
  511.       array    is created when    the range operator is used as the
  512.       expression in    foreach    loops, but older versions of Perl
  513.       might    burn a lot of memory when you write something like
  514.       this:
  515.  
  516.           for (1 ..    1_000_000) {
  517.           # code
  518.           }
  519.  
  520.       In scalar context, ".." returns a boolean value.  The
  521.       operator is bistable,    like a flip-flop, and emulates the
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  533.  
  534.  
  535.  
  536.       line-range (comma) operator of sssseeeedddd, aaaawwwwkkkk, and various
  537.       editors.  Each ".." operator maintains its own boolean
  538.       state.  It is    false as long as its left operand is false.
  539.       Once the left    operand    is true, the range operator stays true
  540.       until    the right operand is true, _A_F_T_E_R which the range
  541.       operator becomes false again.     (It doesn't become false till
  542.       the next time    the range operator is evaluated.  It can test
  543.       the right operand and    become false on    the same evaluation it
  544.       became true (as in aaaawwwwkkkk), but it still    returns    true once.  If
  545.       you don't want it to test the    right operand till the next
  546.       evaluation (as in sssseeeedddd), use three dots ("...") instead of
  547.       two.)     The right operand is not evaluated while the operator
  548.       is in    the "false" state, and the left    operand    is not
  549.       evaluated while the operator is in the "true"    state.    The
  550.       precedence is    a little lower than || and &&.    The value
  551.       returned is either the empty string for false, or a sequence
  552.       number (beginning with 1) for    true.  The sequence number is
  553.       reset    for each range encountered.  The final sequence    number
  554.       in a range has the string "E0" appended to it, which doesn't
  555.       affect its numeric value, but    gives you something to search
  556.       for if you want to exclude the endpoint.  You    can exclude
  557.       the beginning    point by waiting for the sequence number to be
  558.       greater than 1.  If either operand of    scalar ".." is a
  559.       constant expression, that operand is implicitly compared to
  560.       the $. variable, the current line number.  Examples:
  561.  
  562.       As a scalar operator:
  563.  
  564.           if (101 .. 200) {    print; }  # print 2nd hundred lines
  565.           next line    if (1 .. /^$/);      # skip header    lines
  566.           s/^/> / if (/^$/ .. eof()); # quote body
  567.  
  568.           #    parse mail messages
  569.           while (<>) {
  570.           $in_header =     1  .. /^$/;
  571.           $in_body   = /^$/ .. eof();
  572.           # do something based on those
  573.           }    continue {
  574.           close    ARGV if    eof;          # reset $. each file
  575.           }
  576.  
  577.       As a list operator:
  578.  
  579.           for (101 .. 200) { print;    } # print $_ 100 times
  580.           @foo = @foo[0 .. $#foo];      # an expensive no-op
  581.           @foo = @foo[$#foo-4 .. $#foo];      # slice last 5 items
  582.  
  583.       The range operator (in list context) makes use of the
  584.       magical auto-increment algorithm if the operands are
  585.       strings.  You    can say
  586.  
  587.           @alphabet    = ('A' .. 'Z');
  588.  
  589.  
  590.  
  591.      Page 9                        (printed 10/23/98)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  599.  
  600.  
  601.  
  602.       to get all the letters of the    alphabet, or
  603.  
  604.           $hexdigit    = (0 ..    9, 'a' .. 'f')[$num & 15];
  605.  
  606.       to get a hexadecimal digit, or
  607.  
  608.           @z2 = ('01' .. '31');  print $z2[$mday];
  609.  
  610.       to get dates with leading zeros.  If the final value
  611.       specified is not in the sequence that    the magical increment
  612.       would    produce, the sequence goes until the next value    would
  613.       be longer than the final value specified.
  614.  
  615.       CCCCoooonnnnddddiiiittttiiiioooonnnnaaaallll OOOOppppeeeerrrraaaattttoooorrrr
  616.  
  617.       Ternary "?:" is the conditional operator, just as in C.  It
  618.       works    much like an if-then-else.  If the argument before the
  619.       ? is true, the argument before the : is returned, otherwise
  620.       the argument after the :  is returned.  For example:
  621.  
  622.           printf "I    have %d    dog%s.\n", $n,
  623.               ($n == 1)    ? '' : "s";
  624.  
  625.       Scalar or list context propagates downward into the 2nd or
  626.       3rd argument,    whichever is selected.
  627.  
  628.           $a = $ok ? $b : $c;  # get a scalar
  629.           @a = $ok ? @b : @c;  # get an array
  630.           $a = $ok ? @b : @c;  # oops, that's just a count!
  631.  
  632.       The operator may be assigned to if both the 2nd and 3rd
  633.       arguments are    legal lvalues (meaning that you    can assign to
  634.       them):
  635.  
  636.           ($a_or_b ? $a : $b) = $c;
  637.  
  638.       This is not necessarily guaranteed to    contribute to the
  639.       readability of your program.
  640.  
  641.       Because this operator    produces an assignable result, using
  642.       assignments without parentheses will get you in trouble.
  643.       For example, this:
  644.  
  645.           $a % 2 ? $a += 10    : $a +=    2
  646.  
  647.       Really means this:
  648.  
  649.           (($a % 2)    ? ($a += 10) : $a) += 2
  650.  
  651.       Rather than this:
  652.  
  653.           ($a % 2) ? ($a +=    10) : ($a += 2)
  654.  
  655.  
  656.  
  657.      Page 10                        (printed 10/23/98)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  665.  
  666.  
  667.  
  668.       AAAAssssssssiiiiggggnnnnmmmmeeeennnntttt OOOOppppeeeerrrraaaattttoooorrrrssss
  669.  
  670.       "=" is the ordinary assignment operator.
  671.  
  672.       Assignment operators work as in C.  That is,
  673.  
  674.           $a += 2;
  675.  
  676.       is equivalent    to
  677.  
  678.           $a = $a +    2;
  679.  
  680.       although without duplicating any side    effects    that
  681.       dereferencing    the lvalue might trigger, such as from _t_i_e().
  682.       Other    assignment operators work similarly.  The following
  683.       are recognized:
  684.  
  685.           **=    +=       *=     &=    <<=    &&=
  686.              -=       /=     |=    >>=    ||=
  687.              .=       %=     ^=
  688.                x=
  689.  
  690.       Note that while these    are grouped by family, they all    have
  691.       the precedence of assignment.
  692.  
  693.       Unlike in C, the assignment operator produces    a valid
  694.       lvalue.  Modifying an    assignment is equivalent to doing the
  695.       assignment and then modifying    the variable that was assigned
  696.       to.  This is useful for modifying a copy of something, like
  697.       this:
  698.  
  699.           ($tmp = $global) =~ tr [A-Z] [a-z];
  700.  
  701.       Likewise,
  702.  
  703.           ($a += 2)    *= 3;
  704.  
  705.       is equivalent    to
  706.  
  707.           $a += 2;
  708.           $a *= 3;
  709.  
  710.  
  711.       CCCCoooommmmmmmmaaaa    OOOOppppeeeerrrraaaattttoooorrrr
  712.  
  713.       Binary "," is    the comma operator.  In    scalar context it
  714.       evaluates its    left argument, throws that value away, then
  715.       evaluates its    right argument and returns that    value.    This
  716.       is just like C's comma operator.
  717.  
  718.       In list context, it's    just the list argument separator, and
  719.       inserts both its arguments into the list.
  720.  
  721.  
  722.  
  723.      Page 11                        (printed 10/23/98)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  731.  
  732.  
  733.  
  734.       The => digraph is mostly just    a synonym for the comma
  735.       operator.  It's useful for documenting arguments that    come
  736.       in pairs.  As    of release 5.001, it also forces any word to
  737.       the left of it to be interpreted as a    string.
  738.  
  739.       LLLLiiiisssstttt OOOOppppeeeerrrraaaattttoooorrrrssss ((((RRRRiiiigggghhhhttttwwwwaaaarrrrdddd))))
  740.  
  741.       On the right side of a list operator,    it has very low
  742.       precedence, such that    it controls all    comma-separated
  743.       expressions found there.  The    only operators with lower
  744.       precedence are the logical operators "and", "or", and    "not",
  745.       which    may be used to evaluate    calls to list operators
  746.       without the need for extra parentheses:
  747.  
  748.           open HANDLE, "filename"
  749.           or die "Can't    open: $!\n";
  750.  
  751.       See also discussion of list operators    in the section on
  752.       _T_e_r_m_s    _a_n_d _L_i_s_t _O_p_e_r_a_t_o_r_s (_L_e_f_t_w_a_r_d).
  753.  
  754.       LLLLooooggggiiiiccccaaaallll NNNNooootttt
  755.  
  756.       Unary    "not" returns the logical negation of the expression
  757.       to its right.     It's the equivalent of    "!" except for the
  758.       very low precedence.
  759.  
  760.       LLLLooooggggiiiiccccaaaallll AAAAnnnndddd
  761.  
  762.       Binary "and" returns the logical conjunction of the two
  763.       surrounding expressions.  It's equivalent to && except for
  764.       the very low precedence.  This means that it short-circuits:
  765.       i.e.,    the right expression is    evaluated only if the left
  766.       expression is    true.
  767.  
  768.       LLLLooooggggiiiiccccaaaallll oooorrrr aaaannnndddd EEEExxxxcccclllluuuussssiiiivvvveeee OOOOrrrr
  769.  
  770.       Binary "or" returns the logical disjunction of the two
  771.       surrounding expressions.  It's equivalent to || except for
  772.       the very low precedence.  This makes it useful for control
  773.       flow
  774.  
  775.           print FH $data          or die "Can't    write to FH: $!";
  776.  
  777.       This means that it short-circuits: i.e., the right
  778.       expression is    evaluated only if the left expression is
  779.       false.  Due to its precedence, you should probably avoid
  780.       using    this for assignment, only for control flow.
  781.  
  782.           $a = $b or $c;          # bug: this is wrong
  783.           ($a = $b)    or $c;          # really means this
  784.           $a = $b || $c;          # better written this    way
  785.  
  786.  
  787.  
  788.  
  789.      Page 12                        (printed 10/23/98)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  797.  
  798.  
  799.  
  800.       However, when    it's a list context assignment and you're
  801.       trying to use    "||" for control flow, you probably need "or"
  802.       so that the assignment takes higher precedence.
  803.  
  804.           @info = stat($file) || die;     #    oops, scalar sense of stat!
  805.           @info = stat($file) or die;     #    better,    now @info gets its due
  806.  
  807.       Then again, you could    always use parentheses.
  808.  
  809.       Binary "xor" returns the exclusive-OR    of the two surrounding
  810.       expressions.    It cannot short    circuit, of course.
  811.  
  812.       CCCC OOOOppppeeeerrrraaaattttoooorrrrssss MMMMiiiissssssssiiiinnnngggg FFFFrrrroooommmm PPPPeeeerrrrllll
  813.  
  814.       Here is what C has that Perl doesn't:
  815.  
  816.       unary    & Address-of operator.    (But see the "\" operator for
  817.           taking a reference.)
  818.  
  819.       unary    * Dereference-address operator.    (Perl's    prefix
  820.           dereferencing    operators are typed: $,    @, %, and &.)
  821.  
  822.       (TYPE)  Type casting operator.
  823.  
  824.       QQQQuuuuooootttteeee    aaaannnndddd QQQQuuuuooootttteeee----lllliiiikkkkeeee OOOOppppeeeerrrraaaattttoooorrrrssss
  825.  
  826.       While    we usually think of quotes as literal values, in Perl
  827.       they function    as operators, providing    various    kinds of
  828.       interpolating    and pattern matching capabilities.  Perl
  829.       provides customary quote characters for these    behaviors, but
  830.       also provides    a way for you to choose    your quote character
  831.       for any of them.  In the following table, a {} represents
  832.       any pair of delimiters you choose.  Non-bracketing
  833.       delimiters use the same character fore and aft, but the 4
  834.       sorts    of brackets (round, angle, square, curly) will all
  835.       nest.
  836.  
  837.           Customary     Generic    Meaning           Interpolates
  838.           ''       q{}        Literal            no
  839.           ""      qq{}        Literal            yes
  840.           ``      qx{}        Command            yes    (unless    '' is delimiter)
  841.               qw{}           Word list        no
  842.           //       m{}         Pattern match        yes
  843.               qr{}        Pattern            yes
  844.                s{}{}      Substitution        yes
  845.               tr{}{}    Transliteration        no (but see    below)
  846.  
  847.       Note that there can be whitespace between the    operator and
  848.       the quoting characters, except when #    is being used as the
  849.       quoting character.  q#foo# is    parsed as being    the string
  850.       foo, while q #foo# is    the operator q followed    by a comment.
  851.       Its argument will be taken from the next line. This allows
  852.  
  853.  
  854.  
  855.      Page 13                        (printed 10/23/98)
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  863.  
  864.  
  865.  
  866.       you to write:
  867.  
  868.           s    {foo}  # Replace foo
  869.         {bar}  # with bar.
  870.  
  871.       For constructs that do interpolation,    variables beginning
  872.       with "$" or "@" are interpolated, as are the following
  873.       sequences. Within a transliteration, the first ten of    these
  874.       sequences may    be used.
  875.  
  876.           \t      tab          (HT, TAB)
  877.           \n      newline      (NL)
  878.           \r      return      (CR)
  879.           \f      form feed      (FF)
  880.           \b      backspace      (BS)
  881.           \a      alarm    (bell)      (BEL)
  882.           \e      escape      (ESC)
  883.           \033      octal    char
  884.           \x1b      hex char
  885.           \c[      control char
  886.  
  887.           \l      lowercase next char
  888.           \u      uppercase next char
  889.           \L      lowercase till \E
  890.           \U      uppercase till \E
  891.           \E      end case modification
  892.           \Q      quote    non-word characters till \E
  893.  
  894.       If use locale    is in effect, the case map used    by \l, \L, \u
  895.       and \U is taken from the current locale.  See    the _p_e_r_l_l_o_c_a_l_e
  896.       manpage.
  897.  
  898.       All systems use the virtual "\n" to represent    a line
  899.       terminator, called a "newline".  There is no such thing as
  900.       an unvarying,    physical newline character.  It    is an illusion
  901.       that the operating system, device drivers, C libraries, and
  902.       Perl all conspire to preserve.  Not all systems read "\r" as
  903.       ASCII    CR and "\n" as ASCII LF.  For example, on a Mac, these
  904.       are reversed,    and on systems without line terminator,
  905.       printing "\n"    may emit no actual data.  In general, use "\n"
  906.       when you mean    a "newline" for    your system, but use the
  907.       literal ASCII    when you need an exact character.  For
  908.       example, most    networking protocols expect and    prefer a CR+LF
  909.       ("\012\015" or "\cJ\cM") for line terminators, and although
  910.       they often accept just "\012", they seldom tolerate just
  911.       "\015".  If you get in the habit of using "\n" for
  912.       networking, you may be burned    some day.
  913.  
  914.       You cannot include a literal $ or @ within a \Q sequence. An
  915.       unescaped $ or @ interpolates    the corresponding variable,
  916.       while    escaping will cause the    literal    string \$ to be
  917.       inserted.  You'll need to write something like
  918.  
  919.  
  920.  
  921.      Page 14                        (printed 10/23/98)
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  929.  
  930.  
  931.  
  932.       m/\Quser\E\@\Qhost/.
  933.  
  934.       Patterns are subject to an additional    level of
  935.       interpretation as a regular expression.  This    is done    as a
  936.       second pass, after variables are interpolated, so that
  937.       regular expressions may be incorporated into the pattern
  938.       from the variables.  If this is not what you want, use \Q to
  939.       interpolate a    variable literally.
  940.  
  941.       Apart    from the above,    there are no multiple levels of
  942.       interpolation.  In particular, contrary to the expectations
  943.       of shell programmers,    back-quotes do _N_O_T interpolate within
  944.       double quotes, nor do    single quotes impede evaluation    of
  945.       variables when used within double quotes.
  946.  
  947.       RRRReeeeggggeeeexxxxpppp QQQQuuuuooootttteeee----LLLLiiiikkkkeeee OOOOppppeeeerrrraaaattttoooorrrrssss
  948.  
  949.       Here are the quote-like operators that apply to pattern
  950.       matching and related activities.
  951.  
  952.       Most of this section is related to use of regular
  953.       expressions from Perl.  Such a use may be considered from
  954.       two points of    view: Perl handles a a string and a "pattern"
  955.       to RE    (regular expression) engine to match, RE engine    finds
  956.       (or does not find) the match,    and Perl uses the findings of
  957.       RE engine for    its operation, possibly    asking the engine for
  958.       other    matches.
  959.  
  960.       RE engine has    no idea    what Perl is going to do with what it
  961.       finds, similarly, the    rest of    Perl has no idea what a
  962.       particular regular expression    means to RE engine.  This
  963.       creates a clean separation, and in this section we discuss
  964.       matching from    Perl point of view only.  The other point of
  965.       view may be found in the _p_e_r_l_r_e manpage.
  966.  
  967.       ?PATTERN?
  968.           This is just like the    /pattern/ search, except that
  969.           it matches only once between calls to    the _r_e_s_e_t()
  970.           operator.  This is a useful optimization when    you
  971.           want to see only the first occurrence    of something
  972.           in each file of a set    of files, for instance.     Only
  973.           ??  patterns local to    the current package are    reset.
  974.  
  975.               while (<>) {
  976.               if (?^$?) {
  977.                           #    blank line between header and body
  978.               }
  979.               }    continue {
  980.               reset    if eof;          #    clear ?? status    for next file
  981.               }
  982.  
  983.           This usage is    vaguely    deprecated, and    may be removed
  984.  
  985.  
  986.  
  987.      Page 15                        (printed 10/23/98)
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  995.  
  996.  
  997.  
  998.           in some future version of Perl.
  999.  
  1000.       m/PATTERN/cgimosx
  1001.  
  1002.       /PATTERN/cgimosx
  1003.           Searches a string for    a pattern match, and in    scalar
  1004.           context returns true (1) or false ('').  If no
  1005.           string is specified via the =~ or !~ operator, the
  1006.           $_ string is searched.  (The string specified    with
  1007.           =~ need not be an lvalue--it may be the result of an
  1008.           expression evaluation, but remember the =~ binds
  1009.           rather tightly.)  See    also the _p_e_r_l_r_e    manpage.  See
  1010.           the _p_e_r_l_l_o_c_a_l_e manpage for discussion    of additional
  1011.           considerations that apply when use locale is in
  1012.           effect.
  1013.  
  1014.           Options are:
  1015.  
  1016.               c      Do not reset search position on a failed match when /g is in effect.
  1017.               g      Match    globally, i.e.,    find all occurrences.
  1018.               i      Do case-insensitive pattern matching.
  1019.               m      Treat    string as multiple lines.
  1020.               o      Compile pattern only once.
  1021.               s      Treat    string as single line.
  1022.               x      Use extended regular expressions.
  1023.  
  1024.           If "/" is the    delimiter then the initial m is
  1025.           optional.  With the m    you can    use any    pair of    non-
  1026.           alphanumeric,    non-whitespace characters as
  1027.           delimiters (if single    quotes are used, no
  1028.           interpretation is done on the    replacement string.
  1029.           Unlike Perl 4, Perl 5    treats backticks as normal
  1030.           delimiters; the replacement text is not evaluated as
  1031.           a command).  This is particularly useful for
  1032.           matching Unix    path names that    contain    "/", to    avoid
  1033.           LTS (leaning toothpick syndrome).  If    "?" is the
  1034.           delimiter, then the match-only-once rule of
  1035.           ?PATTERN? applies.
  1036.  
  1037.           PATTERN may contain variables, which will be
  1038.           interpolated (and the    pattern    recompiled) every time
  1039.           the pattern search is    evaluated.  (Note that $) and
  1040.           $| might not be interpolated because they look like
  1041.           end-of-string    tests.)     If you    want such a pattern to
  1042.           be compiled only once, add a /o after    the trailing
  1043.           delimiter.  This avoids expensive run-time
  1044.           recompilations, and is useful    when the value you are
  1045.           interpolating    won't change over the life of the
  1046.           script.  However, mentioning /o constitutes a
  1047.           promise that you won't change    the variables in the
  1048.           pattern.  If you change them,    Perl won't even
  1049.           notice.
  1050.  
  1051.  
  1052.  
  1053.      Page 16                        (printed 10/23/98)
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1061.  
  1062.  
  1063.  
  1064.           If the PATTERN evaluates to the empty    string,    the
  1065.           last _s_u_c_c_e_s_s_f_u_l_l_y matched regular expression is used
  1066.           instead.
  1067.  
  1068.           If the /g option is not used,    m// in a list context
  1069.           returns a list consisting of the subexpressions
  1070.           matched by the parentheses in    the pattern, i.e.,
  1071.           ($1, $2, $3...).  (Note that here $1 etc. are    also
  1072.           set, and that    this differs from Perl 4's behavior.)
  1073.           When there are no parentheses    in the pattern,    the
  1074.           return value is the list (1) for success.  With or
  1075.           without parentheses, an empty    list is    returned upon
  1076.           failure.
  1077.  
  1078.           Examples:
  1079.  
  1080.               open(TTY,    '/dev/tty');
  1081.               <TTY> =~ /^y/i &&    foo();      # do foo if desired
  1082.  
  1083.               if (/Version: *([0-9.]*)/) { $version = $1; }
  1084.  
  1085.               next if m#^/usr/spool/uucp#;
  1086.  
  1087.               #    poor man's grep
  1088.               $arg = shift;
  1089.               while (<>) {
  1090.               print    if /$arg/o;      # compile only once
  1091.               }
  1092.  
  1093.               if (($F1,    $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
  1094.  
  1095.           This last example splits $foo    into the first two
  1096.           words    and the    remainder of the line, and assigns
  1097.           those    three fields to    $F1, $F2, and $Etc.  The
  1098.           conditional is true if any variables were assigned,
  1099.           i.e.,    if the pattern matched.
  1100.  
  1101.           The /g modifier specifies global pattern matching--
  1102.           that is, matching as many times as possible within
  1103.           the string.  How it behaves depends on the context.
  1104.           In list context, it returns a    list of    all the
  1105.           substrings matched by    all the    parentheses in the
  1106.           regular expression.  If there    are no parentheses, it
  1107.           returns a list of all    the matched strings, as    if
  1108.           there    were parentheses around    the whole pattern.
  1109.  
  1110.           In scalar context, each execution of m//g finds the
  1111.           next match, returning    TRUE if    it matches, and    FALSE
  1112.           if there is no further match.     The position after
  1113.           the last match can be    read or    set using the _p_o_s()
  1114.           function; see    the pos    entry in the _p_e_r_l_f_u_n_c manpage.
  1115.           A failed match normally resets the search position
  1116.  
  1117.  
  1118.  
  1119.      Page 17                        (printed 10/23/98)
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1127.  
  1128.  
  1129.  
  1130.           to the beginning of the string, but you can avoid
  1131.           that by adding the /c    modifier (e.g. m//gc).
  1132.           Modifying the    target string also resets the search
  1133.           position.
  1134.  
  1135.           You can intermix m//g    matches    with m/\G.../g,    where
  1136.           \G is    a zero-width assertion that matches the    exact
  1137.           position where the previous m//g, if any, left off.
  1138.           The \G assertion is not supported without the    /g
  1139.           modifier; currently, without /g, \G behaves just
  1140.           like \A, but that's accidental and may change    in the
  1141.           future.
  1142.  
  1143.           Examples:
  1144.  
  1145.               #    list context
  1146.               ($one,$five,$fifteen) = (`uptime`    =~ /(\d+\.\d+)/g);
  1147.  
  1148.               #    scalar context
  1149.               $/ = ""; $* = 1;    # $* deprecated    in modern perls
  1150.               while (defined($paragraph    = <>)) {
  1151.               while    ($paragraph =~ /[a-z]['")]*[.!?]+['")]*\s/g) {
  1152.                   $sentences++;
  1153.               }
  1154.               }
  1155.               print "$sentences\n";
  1156.  
  1157.               #    using m//gc with \G
  1158.               $_ = "ppooqppqq";
  1159.               while ($i++ < 2) {
  1160.               print    "1: '";
  1161.               print    $1 while /(o)/gc; print    "', pos=", pos,    "\n";
  1162.               print    "2: '";
  1163.               print    $1 if /\G(q)/gc;  print    "', pos=", pos,    "\n";
  1164.               print    "3: '";
  1165.               print    $1 while /(p)/gc; print    "', pos=", pos,    "\n";
  1166.               }
  1167.  
  1168.           The last example should print:
  1169.  
  1170.               1: 'oo', pos=4
  1171.               2: 'q', pos=5
  1172.               3: 'pp', pos=7
  1173.               1: '', pos=7
  1174.               2: 'q', pos=8
  1175.               3: '', pos=8
  1176.  
  1177.           A useful idiom for lex-like scanners is /\G.../gc.
  1178.           You can combine several regexps like this to process
  1179.           a string part-by-part, doing different actions
  1180.           depending on which regexp matched.  Each regexp
  1181.           tries    to match where the previous one    leaves off.
  1182.  
  1183.  
  1184.  
  1185.      Page 18                        (printed 10/23/98)
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1193.  
  1194.  
  1195.  
  1196.            $_ =    <<'EOL';
  1197.             $url = new URI::URL "http://www/";   die if $url eq "xXx";
  1198.            EOL
  1199.            LOOP:
  1200.               {
  1201.             print("    digits"),      redo LOOP if /\G\d+\b[,.;]?\s*/gc;
  1202.             print("    lowercase"),      redo LOOP if /\G[a-z]+\b[,.;]?\s*/gc;
  1203.             print("    UPPERCASE"),      redo LOOP if /\G[A-Z]+\b[,.;]?\s*/gc;
  1204.             print("    Capitalized"),      redo LOOP if /\G[A-Z][a-z]+\b[,.;]?\s*/gc;
  1205.             print("    MiXeD"),      redo LOOP if /\G[A-Za-z]+\b[,.;]?\s*/gc;
  1206.             print("    alphanumeric"),      redo LOOP if /\G[A-Za-z0-9]+\b[,.;]?\s*/gc;
  1207.             print("    line-noise"),      redo LOOP if /\G[^A-Za-z0-9]+/gc;
  1208.             print ". That's    all!\n";
  1209.               }
  1210.  
  1211.           Here is the output (split into several lines):
  1212.  
  1213.            line-noise lowercase    line-noise lowercase UPPERCASE line-noise
  1214.            UPPERCASE line-noise    lowercase line-noise lowercase line-noise
  1215.            lowercase lowercase line-noise lowercase lowercase line-noise
  1216.            MiXeD line-noise. That's all!
  1217.  
  1218.  
  1219.       q/STRING/
  1220.  
  1221.       'STRING'
  1222.           A single-quoted, literal string. A backslash
  1223.           represents a backslash unless    followed by the
  1224.           delimiter or another backslash, in which case    the
  1225.           delimiter or backslash is interpolated.
  1226.  
  1227.               $foo = q!I said, "You said, 'She said it.'"!;
  1228.               $bar = q('This is    it.');
  1229.               $baz = '\n';          # a two-character string
  1230.  
  1231.  
  1232.       qq/STRING/
  1233.  
  1234.       "STRING"
  1235.           A double-quoted, interpolated    string.
  1236.  
  1237.               $_ .= qq
  1238.                (*** The    previous line contains the naughty word    "$1".\n)
  1239.                   if /(tcl|rexx|python)/;      # :-)
  1240.               $baz = "\n";          # a one-character string
  1241.  
  1242.  
  1243.       qr/STRING/imosx
  1244.           A string which is (possibly) interpolated and    then
  1245.           compiled as a    regular    expression. The    result may be
  1246.           used as a pattern in a match
  1247.  
  1248.  
  1249.  
  1250.  
  1251.      Page 19                        (printed 10/23/98)
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1259.  
  1260.  
  1261.  
  1262.               $re = qr/$pattern/;
  1263.               $string =~ /foo${re}bar/;      # can    be interpolated    in other patterns
  1264.               $string =~ $re;          # or used standalone
  1265.  
  1266.           Options are:
  1267.  
  1268.               i      Do case-insensitive pattern matching.
  1269.               m      Treat    string as multiple lines.
  1270.               o      Compile pattern only once.
  1271.               s      Treat    string as single line.
  1272.               x      Use extended regular expressions.
  1273.  
  1274.           The benefit from this    is that    the pattern is
  1275.           precompiled into an internal representation, and
  1276.           does not need    to be recompiled every time a match is
  1277.           attempted.  This makes it very efficient to do
  1278.           something like:
  1279.  
  1280.               foreach $pattern (@pattern_list) {
  1281.               my $re = qr/$pattern/;
  1282.               foreach $line    (@lines) {
  1283.                   if($line =~ /$re/) {
  1284.                   do_something($line);
  1285.                   }
  1286.               }
  1287.               }
  1288.  
  1289.           See the _p_e_r_l_r_e manpage for additional    information on
  1290.           valid    syntax for STRING, and for a detailed look at
  1291.           the semantics    of regular expressions.
  1292.  
  1293.       qx/STRING/
  1294.  
  1295.       `STRING`
  1296.           A string which is (possibly) interpolated and    then
  1297.           executed as a    system command with /bin/sh or its
  1298.           equivalent.  Shell wildcards,    pipes, and
  1299.           redirections will be honored.     The collected
  1300.           standard output of the command is returned; standard
  1301.           error    is unaffected.    In scalar context, it comes
  1302.           back as a single (potentially    multi-line) string.
  1303.           In list context, returns a list of lines (however
  1304.           you've defined lines with $/ or
  1305.           $INPUT_RECORD_SEPARATOR).
  1306.  
  1307.           Because backticks do not affect standard error, use
  1308.           shell    file descriptor    syntax (assuming the shell
  1309.           supports this) if you    care to    address    this.  To
  1310.           capture a command's STDERR and STDOUT    together:
  1311.  
  1312.               $output =    `cmd 2>&1`;
  1313.  
  1314.  
  1315.  
  1316.  
  1317.      Page 20                        (printed 10/23/98)
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1325.  
  1326.  
  1327.  
  1328.           To capture a command's STDOUT    but discard its
  1329.           STDERR:
  1330.  
  1331.               $output =    `cmd 2>/dev/null`;
  1332.  
  1333.           To capture a command's STDERR    but discard its    STDOUT
  1334.           (ordering is important here):
  1335.  
  1336.               $output =    `cmd 2>&1 1>/dev/null`;
  1337.  
  1338.           To exchange a    command's STDOUT and STDERR in order
  1339.           to capture the STDERR    but leave its STDOUT to    come
  1340.           out the old STDERR:
  1341.  
  1342.               $output =    `cmd 3>&1 1>&2 2>&3 3>&-`;
  1343.  
  1344.           To read both a command's STDOUT and its STDERR
  1345.           separately, it's easiest and safest to redirect them
  1346.           separately to    files, and then    read from those    files
  1347.           when the program is done:
  1348.  
  1349.               system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr");
  1350.  
  1351.           Using    single-quote as    a delimiter protects the
  1352.           command from Perl's double-quote interpolation,
  1353.           passing it on    to the shell instead:
  1354.  
  1355.               $perl_info  = qx(ps $$);          # that's Perl's $$
  1356.               $shell_info = qx'ps $$';          # that's the new shell's $$
  1357.  
  1358.           Note that how    the string gets    evaluated is entirely
  1359.           subject to the command interpreter on    your system.
  1360.           On most platforms, you will have to protect shell
  1361.           metacharacters if you    want them treated literally.
  1362.           This is in practice difficult    to do, as it's unclear
  1363.           how to escape    which characters.  See the _p_e_r_l_s_e_c
  1364.           manpage for a    clean and safe example of a manual
  1365.           _f_o_r_k() and _e_x_e_c() to emulate backticks safely.
  1366.  
  1367.           On some platforms (notably DOS-like ones), the shell
  1368.           may not be capable of    dealing    with multiline
  1369.           commands, so putting newlines    in the string may not
  1370.           get you what you want.  You may be able to evaluate
  1371.           multiple commands in a single    line by    separating
  1372.           them with the    command    separator character, if    your
  1373.           shell    supports that (e.g. ; on many Unix shells; &
  1374.           on the Windows NT cmd    shell).
  1375.  
  1376.           Beware that some command shells may place
  1377.           restrictions on the length of    the command line.  You
  1378.           must ensure your strings don't exceed    this limit
  1379.           after    any necessary interpolations.  See the
  1380.  
  1381.  
  1382.  
  1383.      Page 21                        (printed 10/23/98)
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1391.  
  1392.  
  1393.  
  1394.           platform-specific release notes for more details
  1395.           about    your particular    environment.
  1396.  
  1397.           Using    this operator can lead to programs that    are
  1398.           difficult to port, because the shell commands    called
  1399.           vary between systems,    and may    in fact    not be present
  1400.           at all.  As one example, the type command under the
  1401.           POSIX    shell is very different    from the type command
  1402.           under    DOS.  That doesn't mean    you should go out of
  1403.           your way to avoid backticks when they're the right
  1404.           way to get something done.  Perl was made to be a
  1405.           glue language, and one of the    things it glues
  1406.           together is commands.     Just understand what you're
  1407.           getting yourself into.
  1408.  
  1409.           See the section on _I/_O _O_p_e_r_a_t_o_r_s for more
  1410.           discussion.
  1411.  
  1412.       qw/STRING/
  1413.           Returns a list of the    words extracted    out of STRING,
  1414.           using    embedded whitespace as the word    delimiters.
  1415.           It is    exactly    equivalent to
  1416.  
  1417.               split(' ', q/STRING/);
  1418.  
  1419.           This equivalency means that if used in scalar
  1420.           context, you'll get split's (unfortunate) scalar
  1421.           context behavior, complete with mysterious warnings.
  1422.  
  1423.           Some frequently seen examples:
  1424.  
  1425.               use POSIX    qw( setlocale localeconv )
  1426.               @EXPORT =    qw( foo    bar baz    );
  1427.  
  1428.           A common mistake is to try to    separate the words
  1429.           with comma or    to put comments    into a multi-line
  1430.           qw-string.  For this reason the -w switch produce
  1431.           warnings if the STRING contains the "," or the "#"
  1432.           character.
  1433.  
  1434.       s/PATTERN/REPLACEMENT/egimosx
  1435.           Searches a string for    a pattern, and if found,
  1436.           replaces that    pattern    with the replacement text and
  1437.           returns the number of    substitutions made.  Otherwise
  1438.           it returns false (specifically, the empty string).
  1439.  
  1440.           If no    string is specified via    the =~ or !~ operator,
  1441.           the $_ variable is searched and modified.  (The
  1442.           string specified with    =~ must    be scalar variable, an
  1443.           array    element, a hash    element, or an assignment to
  1444.           one of those,    i.e., an lvalue.)
  1445.  
  1446.  
  1447.  
  1448.  
  1449.      Page 22                        (printed 10/23/98)
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1457.  
  1458.  
  1459.  
  1460.           If the delimiter chosen is single quote, no variable
  1461.           interpolation    is done    on either the PATTERN or the
  1462.           REPLACEMENT.    Otherwise, if the PATTERN contains a $
  1463.           that looks like a variable rather than an end-of-
  1464.           string test, the variable will be interpolated into
  1465.           the pattern at run-time.  If you want    the pattern
  1466.           compiled only    once the first time the    variable is
  1467.           interpolated,    use the    /o option.  If the pattern
  1468.           evaluates to the empty string, the last successfully
  1469.           executed regular expression is used instead.    See
  1470.           the _p_e_r_l_r_e manpage for further explanation on    these.
  1471.           See the _p_e_r_l_l_o_c_a_l_e manpage for discussion of
  1472.           additional considerations that apply when use    locale
  1473.           is in    effect.
  1474.  
  1475.           Options are:
  1476.  
  1477.               e      Evaluate the right side as an    expression.
  1478.               g      Replace globally, i.e., all occurrences.
  1479.               i      Do case-insensitive pattern matching.
  1480.               m      Treat    string as multiple lines.
  1481.               o      Compile pattern only once.
  1482.               s      Treat    string as single line.
  1483.               x      Use extended regular expressions.
  1484.  
  1485.           Any non-alphanumeric,    non-whitespace delimiter may
  1486.           replace the slashes.    If single quotes are used, no
  1487.           interpretation is done on the    replacement string
  1488.           (the /e modifier overrides this, however).  Unlike
  1489.           Perl 4, Perl 5 treats    backticks as normal
  1490.           delimiters; the replacement text is not evaluated as
  1491.           a command.  If the PATTERN is    delimited by
  1492.           bracketing quotes, the REPLACEMENT has its own pair
  1493.           of quotes, which may or may not be bracketing
  1494.           quotes, e.g.,    s(foo)(bar) or s<foo>/bar/.  A /e will
  1495.           cause    the replacement    portion    to be interpreted as a
  1496.           full-fledged Perl expression and _e_v_a_l()ed right then
  1497.           and there.  It is, however, syntax checked at
  1498.           compile-time.
  1499.  
  1500.           Examples:
  1501.  
  1502.               s/\bgreen\b/mauve/g;          # don't change wintergreen
  1503.  
  1504.               $path =~ s|/usr/bin|/usr/local/bin|;
  1505.  
  1506.               s/Login: $foo/Login: $bar/; # run-time pattern
  1507.  
  1508.               ($foo = $bar) =~ s/this/that/;      # copy first,    then change
  1509.  
  1510.               $count = ($paragraph =~ s/Mister\b/Mr./g);  # get    change-count
  1511.  
  1512.  
  1513.  
  1514.  
  1515.      Page 23                        (printed 10/23/98)
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1523.  
  1524.  
  1525.  
  1526.               $_ = 'abc123xyz';
  1527.               s/\d+/$&*2/e;          # yields 'abc246xyz'
  1528.               s/\d+/sprintf("%5d",$&)/e;  # yields 'abc     246xyz'
  1529.               s/\w/$& x    2/eg;          # yields 'aabbcc  224466xxyyzz'
  1530.  
  1531.               s/%(.)/$percent{$1}/g;      # change percent escapes; no /e
  1532.               s/%(.)/$percent{$1} || $&/ge;      # expr now, so /e
  1533.               s/^=(\w+)/&pod($1)/ge;      # use    function call
  1534.  
  1535.               #    expand variables in $_,    but dynamics only, using
  1536.               #    symbolic dereferencing
  1537.               s/\$(\w+)/${$1}/g;
  1538.  
  1539.               #    /e's can even nest;  this will expand
  1540.               #    any embedded scalar variable (including    lexicals) in $_
  1541.               s/(\$\w+)/$1/eeg;
  1542.  
  1543.               #    Delete (most) C    comments.
  1544.               $program =~ s {
  1545.               /\*      # Match the opening delimiter.
  1546.               .*?      # Match a minimal number of characters.
  1547.               \*/      # Match the closing delimiter.
  1548.               }    []gsx;
  1549.  
  1550.               s/^\s*(.*?)\s*$/$1/;      # trim white space in    $_, expensively
  1551.  
  1552.               for ($variable) {          # trim white space in    $variable, cheap
  1553.               s/^\s+//;
  1554.               s/\s+$//;
  1555.               }
  1556.  
  1557.               s/([^ ]*)    *([^ ]*)/$2 $1/;  # reverse 1st    two fields
  1558.  
  1559.           Note the use of $ instead of \ in the    last example.
  1560.           Unlike sssseeeedddd, we use the \<_d_i_g_i_t> form in only the
  1561.           left hand side.  Anywhere else it's $<_d_i_g_i_t>.
  1562.  
  1563.           Occasionally,    you can't use just a /g    to get all the
  1564.           changes to occur.  Here are two common cases:
  1565.  
  1566.               #    put commas in the right    places in an integer
  1567.               1    while s/(.*\d)(\d\d\d)/$1,$2/g;         # perl4
  1568.               1    while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;  # perl5
  1569.  
  1570.               #    expand tabs to 8-column    spacing
  1571.               1    while s/\t+/' '    x (length($&)*8    - length($`)%8)/e;
  1572.  
  1573.  
  1574.       tr/SEARCHLIST/REPLACEMENTLIST/cds
  1575.  
  1576.       y/SEARCHLIST/REPLACEMENTLIST/cds
  1577.           Transliterates all occurrences of the    characters
  1578.  
  1579.  
  1580.  
  1581.      Page 24                        (printed 10/23/98)
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1589.  
  1590.  
  1591.  
  1592.           found    in the search list with    the corresponding
  1593.           character in the replacement list.  It returns the
  1594.           number of characters replaced    or deleted.  If    no
  1595.           string is specified via the =~ or !~ operator, the
  1596.           $_ string is transliterated.    (The string specified
  1597.           with =~ must be a scalar variable, an    array element,
  1598.           a hash element, or an    assignment to one of those,
  1599.           i.e.,    an lvalue.)  A character range may be
  1600.           specified with a hyphen, so tr/A-J/0-9/ does the
  1601.           same replacement as tr/ACEGIBDFHJ/0246813579/.  For
  1602.           sssseeeedddd devotees,    y is provided as a synonym for tr.  If
  1603.           the SEARCHLIST is delimited by bracketing quotes,
  1604.           the REPLACEMENTLIST has its own pair of quotes,
  1605.           which    may or may not be bracketing quotes, e.g.,
  1606.           tr[A-Z][a-z] or tr(+\-*/)/ABCD/.
  1607.  
  1608.           Options:
  1609.  
  1610.               c      Complement the SEARCHLIST.
  1611.               d      Delete found but unreplaced characters.
  1612.               s      Squash duplicate replaced characters.
  1613.  
  1614.           If the /c modifier is    specified, the SEARCHLIST
  1615.           character set    is complemented.  If the /d modifier
  1616.           is specified,    any characters specified by SEARCHLIST
  1617.           not found in REPLACEMENTLIST are deleted.  (Note
  1618.           that this is slightly    more flexible than the
  1619.           behavior of some ttttrrrr programs,    which delete anything
  1620.           they find in the SEARCHLIST, period.)     If the    /s
  1621.           modifier is specified, sequences of characters that
  1622.           were transliterated to the same character are
  1623.           squashed down    to a single instance of    the character.
  1624.  
  1625.           If the /d modifier is    used, the REPLACEMENTLIST is
  1626.           always interpreted exactly as    specified.  Otherwise,
  1627.           if the REPLACEMENTLIST is shorter than the
  1628.           SEARCHLIST, the final    character is replicated    till
  1629.           it is    long enough.  If the REPLACEMENTLIST is    empty,
  1630.           the SEARCHLIST is replicated.     This latter is    useful
  1631.           for counting characters in a class or    for squashing
  1632.           character sequences in a class.
  1633.  
  1634.           Examples:
  1635.  
  1636.               $ARGV[1] =~ tr/A-Z/a-z/;      # canonicalize to lower case
  1637.  
  1638.               $cnt = tr/*/*/;          # count the stars in $_
  1639.  
  1640.               $cnt = $sky =~ tr/*/*/;      # count the stars in $sky
  1641.  
  1642.               $cnt = tr/0-9//;          # count the digits in    $_
  1643.  
  1644.  
  1645.  
  1646.  
  1647.      Page 25                        (printed 10/23/98)
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1655.  
  1656.  
  1657.  
  1658.               tr/a-zA-Z//s;          # bookkeeper -> bokeper
  1659.  
  1660.               ($HOST = $host) =~ tr/a-z/A-Z/;
  1661.  
  1662.               tr/a-zA-Z/ /cs;          # change non-alphas to single    space
  1663.  
  1664.               tr [\200-\377]
  1665.              [\000-\177];          # delete 8th bit
  1666.  
  1667.           If multiple transliterations are given for a
  1668.           character, only the first one    is used:
  1669.  
  1670.               tr/AAA/XYZ/
  1671.  
  1672.           will transliterate any A to X.
  1673.  
  1674.           Note that because the    transliteration    table is built
  1675.           at compile time, neither the SEARCHLIST nor the
  1676.           REPLACEMENTLIST are subjected    to double quote
  1677.           interpolation.  That means that if you want to use
  1678.           variables, you must use an _e_v_a_l():
  1679.  
  1680.               eval "tr/$oldlist/$newlist/";
  1681.               die $@ if    $@;
  1682.  
  1683.               eval "tr/$oldlist/$newlist/, 1" or die $@;
  1684.  
  1685.  
  1686.       GGGGoooorrrryyyy ddddeeeettttaaaaiiiillllssss ooooffff ppppaaaarrrrssssiiiinnnngggg qqqquuuuooootttteeeedddd ccccoooonnnnssssttttrrrruuuuccccttttssss
  1687.  
  1688.       When presented with something    which may have several
  1689.       different interpretations, Perl uses the principle DDDDWWWWIIIIMMMM
  1690.       (expanded to Do What I Mean -    not what I wrote) to pick up
  1691.       the most probable interpretation of the source.  This
  1692.       strategy is so successful that Perl users usually do not
  1693.       suspect ambivalence of what they write.  However, time to
  1694.       time Perl's ideas differ from    what the author    meant.
  1695.  
  1696.       The target of    this section is    to clarify the Perl's way of
  1697.       interpreting quoted constructs.  The most frequent reason
  1698.       one may have to want to know the details discussed in    this
  1699.       section is hairy regular expressions.     However, the first
  1700.       steps    of parsing are the same    for all    Perl quoting
  1701.       operators, so    here they are discussed    together.
  1702.  
  1703.       Some of the passes discussed below are performed
  1704.       concurrently,    but as far as results are the same, we
  1705.       consider them    one-by-one.  For different quoting constructs
  1706.       Perl performs    different number of passes, from one to    five,
  1707.       but they are always performed    in the same order.
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.      Page 26                        (printed 10/23/98)
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1721.  
  1722.  
  1723.  
  1724.       Finding the end
  1725.            First pass is finding the end of    the quoted construct,
  1726.            be it multichar ender "\nEOF\n" of <<EOF    construct, /
  1727.            which terminates    qq/ construct, ] which terminates qq[
  1728.            construct, or > which terminates    a fileglob started
  1729.            with <.
  1730.  
  1731.            When searching for multichar construct no skipping is
  1732.            performed.  When    searching for one-char non-matching
  1733.            delimiter, such as /, combinations \\ and \/ are
  1734.            skipped.     When searching    for one-char matching
  1735.            delimiter, such as ], combinations \\, \] and \[    are
  1736.            skipped,    and nested [, ]    are skipped as well.
  1737.  
  1738.            For 3-parts constructs, s/// etc. the search is
  1739.            repeated    once more.
  1740.  
  1741.            During this search no attention is paid to the semantic
  1742.            of the construct, thus
  1743.  
  1744.            "$hash{"$foo/$bar"}"
  1745.  
  1746.            or
  1747.  
  1748.            m/
  1749.              bar       #  This is not a    comment, this slash / terminated m//!
  1750.             /x
  1751.  
  1752.            do not form legal quoted    expressions.  Note that    since
  1753.            the slash which terminated m// was followed by a    SPACE,
  1754.            this is not m//x, thus #    was interpreted    as a literal
  1755.            #.
  1756.  
  1757.       Removal of backslashes before    delimiters
  1758.            During the second pass the text between the starting
  1759.            delimiter and the ending    delimiter is copied to a safe
  1760.            location, and the \ is removed from combinations
  1761.            consisting of \ and _d_e_l_i_m_i_t_e_r(s)    (both starting and
  1762.            ending delimiter    if they    differ).
  1763.  
  1764.            The removal does    not happen for multi-char delimiters.
  1765.  
  1766.            Note that the combination \\ is left as it was!
  1767.  
  1768.            Starting    from this step no information about the
  1769.            _d_e_l_i_m_i_t_e_r(s) is used in the parsing.
  1770.  
  1771.       Interpolation
  1772.            Next step is interpolation in the obtained delimiter-
  1773.            independent text.  There    are four different cases.
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779.      Page 27                        (printed 10/23/98)
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1787.  
  1788.  
  1789.  
  1790.       <<'EOF', m'',    s''', tr///, y///
  1791.             No interpolation is    performed.
  1792.  
  1793.       '', q//   The    only interpolation is removal of \ from    pairs
  1794.             \\.
  1795.  
  1796.             \Q,    \U, \u,    \L, \l (possibly paired    with \E) are
  1797.             converted to corresponding Perl constructs,    thus
  1798.             "$foo\Qbaz$bar" is converted to
  1799.  
  1800.                $foo . (quotemeta("baz" . $bar));
  1801.  
  1802.             Other combinations of \ with following chars are
  1803.             substituted    with appropriate expansions.
  1804.  
  1805.             Interpolated scalars and arrays are    converted to
  1806.             join and . Perl constructs,    thus "'@arr'" becomes
  1807.  
  1808.               "'" . (join $", @arr) . "'";
  1809.  
  1810.             Since all three above steps    are performed
  1811.             simultaneously left-to-right, the is no way    to
  1812.             insert a literal $ or @ inside \Q\E    pair: it
  1813.             cannot be protected    by \, since any    \ (except in
  1814.             \E)    is interpreted as a literal inside \Q\E, and
  1815.             any    $ is interpreted as starting an    interpolated
  1816.             scalar.
  1817.  
  1818.             Note also that the interpolating code needs    to
  1819.             make decision where    the interpolated scalar    ends,
  1820.             say, whether "a $b -> {c}" means
  1821.  
  1822.               "a " . $b    . " -> {c}";
  1823.  
  1824.             or
  1825.  
  1826.               "a " . $b    -> {c};
  1827.  
  1828.             Most the time the decision is to take the longest
  1829.             possible text which    does not include spaces
  1830.             between components and contains matching
  1831.             braces/brackets.
  1832.  
  1833.       ?RE?,    /RE/, m/RE/, s/RE/foo/,
  1834.             Processing of \Q, \U, \u, \L, \l and interpolation
  1835.             happens (almost) as    with qq// constructs, but _t_h_e
  1836.             _s_u_b_s_t_i_t_u_t_i_o_n _o_f \ followed by other    chars is not
  1837.             performed!    Moreover, inside (?{BLOCK}) no
  1838.             processing is performed at all.
  1839.  
  1840.             Interpolation has several quirks: $|, $( and $)
  1841.             are    not interpolated, and constructs
  1842.  
  1843.  
  1844.  
  1845.      Page 28                        (printed 10/23/98)
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1853.  
  1854.  
  1855.  
  1856.             $var[SOMETHING] are    _v_o_t_e_d (by several different
  1857.             estimators)    to be an array element or $var
  1858.             followed by    a RE alternative.  This    is the place
  1859.             where the notation ${arr[$bar]} comes handy:
  1860.             /${arr[0-9]}/ is interpreted as an array element
  1861.             -9,    not as a regular expression from variable $arr
  1862.             followed by    a digit, which is the interpretation
  1863.             of /$arr[0-9]/.
  1864.  
  1865.             Note that absence of processing of \\ creates
  1866.             specific restrictions on the post-processed    text:
  1867.             if the delimiter is    /, one cannot get the
  1868.             combination    \/ into    the result of this step: /
  1869.             will finish    the regular expression,    \/ will    be
  1870.             stripped to    / on the previous step,    and \\/    will
  1871.             be left as is.  Since / is equivalent to \/    inside
  1872.             a regular expression, this does not    matter unless
  1873.             the    delimiter is special character for the RE
  1874.             engine, as in s*foo*bar*, m[foo], or ?foo?.
  1875.  
  1876.             This step is the last one for all the constructs
  1877.             except regular expressions,    which are processed
  1878.             further.
  1879.  
  1880.       Interpolation    of regular expressions
  1881.            All the previous    steps were performed during the
  1882.            compilation of Perl code, this one happens in run time
  1883.            (though it may be optimized to be calculated at compile
  1884.            time if appropriate).  After all    the preprocessing
  1885.            performed above (and possibly after evaluation if
  1886.            catenation, joining, up/down-casing and quotemeta()ing
  1887.            are involved) the resulting _s_t_r_i_n_g is passed to RE
  1888.            engine for compilation.
  1889.  
  1890.            Whatever    happens    in the RE engine is better be
  1891.            discussed in the    _p_e_r_l_r_e manpage,    but for    the sake of
  1892.            continuity let us do it here.
  1893.  
  1894.            This is the first step where presence of    the //x    switch
  1895.            is relevant.  The RE engine scans the string left-to-
  1896.            right, and converts it to a finite automaton.
  1897.  
  1898.            Backslashed chars are either substituted    by
  1899.            corresponding literal strings, or generate special
  1900.            nodes of    the finite automaton.  Characters which    are
  1901.            special to the RE engine    generate corresponding nodes.
  1902.            (?#...)    comments are ignored.  All the rest is either
  1903.            converted to literal strings to match, or is ignored
  1904.            (as is whitespace and #-style comments if //x is
  1905.            present).
  1906.  
  1907.            Note that the parsing of    the construct [...] is
  1908.  
  1909.  
  1910.  
  1911.      Page 29                        (printed 10/23/98)
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1919.  
  1920.  
  1921.  
  1922.            performed using absolutely different rules than the
  1923.            rest of the regular expression. Similarly, the (?{...})
  1924.            is only checked for matching braces.
  1925.  
  1926.       Optimization of regular expressions
  1927.            This step is listed for completeness only.  Since it
  1928.            does not    change semantics, details of this step are not
  1929.            documented and are subject to change.
  1930.  
  1931.       IIII////OOOO OOOOppppeeeerrrraaaattttoooorrrrssss
  1932.  
  1933.       There    are several I/O    operators you should know about.  A
  1934.       string enclosed by backticks (grave accents) first undergoes
  1935.       variable substitution    just like a double quoted string.  It
  1936.       is then interpreted as a command, and    the output of that
  1937.       command is the value of the pseudo-literal, like in a    shell.
  1938.       In scalar context, a single string consisting    of all the
  1939.       output is returned.  In list context,    a list of values is
  1940.       returned, one    for each line of output.  (You can set $/ to
  1941.       use a    different line terminator.)  The command is executed
  1942.       each time the    pseudo-literal is evaluated.  The status value
  1943.       of the command is returned in    $? (see    the _p_e_r_l_v_a_r manpage
  1944.       for the interpretation of $?).  Unlike in ccccsssshhhh, no
  1945.       translation is done on the return data--newlines remain
  1946.       newlines.  Unlike in any of the shells, single quotes    do not
  1947.       hide variable    names in the command from interpretation.  To
  1948.       pass a $ through to the shell    you need to hide it with a
  1949.       backslash.  The generalized form of backticks    is qx//.
  1950.       (Because backticks always undergo shell expansion as well,
  1951.       see the _p_e_r_l_s_e_c manpage for security concerns.)
  1952.  
  1953.       Evaluating a filehandle in angle brackets yields the next
  1954.       line from that file (newline,    if any,    included), or undef at
  1955.       end of file.    Ordinarily you must assign that    value to a
  1956.       variable, but    there is one situation where an    automatic
  1957.       assignment happens.  _I_f _a_n_d _O_N_L_Y _i_f the input    symbol is the
  1958.       only thing inside the    conditional of a while or for(;;)
  1959.       loop,    the value is automatically assigned to the variable
  1960.       $_.  In these    loop constructs, the assigned value (whether
  1961.       assignment is    automatic or explicit) is then tested to see
  1962.       if it    is defined.  The defined test avoids problems where
  1963.       line has a string value that would be    treated    as false by
  1964.       perl e.g. "" or "0" with no trailing newline.    (This may seem
  1965.       like an odd thing to you, but    you'll use the construct in
  1966.       almost every Perl script you write.) Anyway, the following
  1967.       lines    are equivalent to each other:
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.      Page 30                        (printed 10/23/98)
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1985.  
  1986.  
  1987.  
  1988.           while (defined($_    = <STDIN>)) { print; }
  1989.           while ($_    = <STDIN>) { print; }
  1990.           while (<STDIN>) {    print; }
  1991.           for (;<STDIN>;) {    print; }
  1992.           print while defined($_ = <STDIN>);
  1993.           print while ($_ =    <STDIN>);
  1994.           print while <STDIN>;
  1995.  
  1996.       and this also    behaves    similarly, but avoids the use of $_ :
  1997.  
  1998.           while (my    $line =    <STDIN>) { print $line }
  1999.  
  2000.       If you really    mean such values to terminate the loop they
  2001.       should be tested for explicitly:
  2002.  
  2003.           while (($_ = <STDIN>) ne '0') { ... }
  2004.           while (<STDIN>) {    last unless $_;    ... }
  2005.  
  2006.       In other boolean contexts, <_f_i_l_e_h_a_n_d_l_e> without explicit
  2007.       defined test or comparison will solicit a warning if -w is
  2008.       in effect.
  2009.  
  2010.       The filehandles STDIN, STDOUT, and STDERR are    predefined.
  2011.       (The filehandles stdin, stdout, and stderr will also work
  2012.       except in packages, where they would be interpreted as local
  2013.       identifiers rather than global.)  Additional filehandles may
  2014.       be created with the _o_p_e_n() function.    See the    open() entry
  2015.       in the _p_e_r_l_f_u_n_c manpage for details on this.
  2016.  
  2017.       If a <FILEHANDLE> is used in a context that is looking for a
  2018.       list,    a list consisting of all the input lines is returned,
  2019.       one line per list element.  It's easy    to make    a _L_A_R_G_E    data
  2020.       space    this way, so use with care.
  2021.  
  2022.       The null filehandle <> is special and    can be used to emulate
  2023.       the behavior of sssseeeedddd and aaaawwwwkkkk.    Input from <> comes either
  2024.       from standard    input, or from each file listed    on the command
  2025.       line.     Here's    how it works: the first    time <>    is evaluated,
  2026.       the @ARGV array is checked, and if it    is empty, $ARGV[0] is
  2027.       set to "-", which when opened    gives you standard input.  The
  2028.       @ARGV    array is then processed    as a list of filenames.     The
  2029.       loop
  2030.  
  2031.           while (<>) {
  2032.           ...              # code for each line
  2033.           }
  2034.  
  2035.       is equivalent    to the following Perl-like pseudo code:
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.  
  2042.  
  2043.      Page 31                        (printed 10/23/98)
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  2051.  
  2052.  
  2053.  
  2054.           unshift(@ARGV, '-') unless @ARGV;
  2055.           while ($ARGV = shift) {
  2056.           open(ARGV, $ARGV);
  2057.           while    (<ARGV>) {
  2058.               ...      # code for each line
  2059.           }
  2060.           }
  2061.  
  2062.       except that it isn't so cumbersome to    say, and will actually
  2063.       work.     It really does    shift array @ARGV and put the current
  2064.       filename into    variable $ARGV.     It also uses filehandle _A_R_G_V
  2065.       internally--<> is just a synonym for <ARGV>, which is
  2066.       magical.  (The pseudo    code above doesn't work    because    it
  2067.       treats <ARGV>    as non-magical.)
  2068.  
  2069.       You can modify @ARGV before the first    <> as long as the
  2070.       array    ends up    containing the list of filenames you really
  2071.       want.     Line numbers ($.)  continue as    if the input were one
  2072.       big happy file.  (But    see example under eof for how to reset
  2073.       line numbers on each file.)
  2074.  
  2075.       If you want to set @ARGV to your own list of files, go right
  2076.       ahead. This sets @ARGV to all    plain text files if no @ARGV
  2077.       was given:
  2078.  
  2079.           @ARGV = grep { -f    && -T }    glob('*') unless @ARGV;
  2080.  
  2081.       You can even set them    to pipe    commands.  For example,    this
  2082.       automatically    filters    compressed arguments through ggggzzzziiiipppp:
  2083.  
  2084.           @ARGV = map { /\.(gz|Z)$/    ? "gzip    -dc < $_ |" : $_ } @ARGV;
  2085.  
  2086.       If you want to pass switches into your script, you can use
  2087.       one of the Getopts modules or    put a loop on the front    like
  2088.       this:
  2089.  
  2090.           while ($_    = $ARGV[0], /^-/) {
  2091.           shift;
  2092.           last if /^--$/;
  2093.           if (/^-D(.*)/) { $debug = $1 }
  2094.           if (/^-v/)     { $verbose++  }
  2095.           # ...          # other switches
  2096.           }
  2097.  
  2098.           while (<>) {
  2099.           # ...          # code for each line
  2100.           }
  2101.  
  2102.       The <> symbol    will return undef for end-of-file only once.
  2103.       If you call it again after this it will assume you are
  2104.       processing another @ARGV list, and if    you haven't set    @ARGV,
  2105.       will input from STDIN.
  2106.  
  2107.  
  2108.  
  2109.      Page 32                        (printed 10/23/98)
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  2117.  
  2118.  
  2119.  
  2120.       If the string    inside the angle brackets is a reference to a
  2121.       scalar variable (e.g., <$foo>), then that variable contains
  2122.       the name of the filehandle to    input from, or its typeglob,
  2123.       or a reference to the    same.  For example:
  2124.  
  2125.           $fh = \*STDIN;
  2126.           $line = <$fh>;
  2127.  
  2128.       If what's within the angle brackets is neither a filehandle
  2129.       nor a    simple scalar variable containing a filehandle name,
  2130.       typeglob, or typeglob    reference, it is interpreted as    a
  2131.       filename pattern to be globbed, and either a list of
  2132.       filenames or the next    filename in the    list is    returned,
  2133.       depending on context.      This distinction is determined on
  2134.       syntactic grounds alone.  That means <$x> is always a
  2135.       readline from    an indirect handle, but    <$hash{key}> is    always
  2136.       a glob.  That's because $x is    a simple scalar    variable, but
  2137.       $hash{key} is    not--it's a hash element.
  2138.  
  2139.       One level of double-quote interpretation is done first, but
  2140.       you can't say    <$foo> because that's an indirect filehandle
  2141.       as explained in the previous paragraph.  (In older versions
  2142.       of Perl, programmers would insert curly brackets to force
  2143.       interpretation as a filename glob:  <${foo}>.     These days,
  2144.       it's considered cleaner to call the internal function
  2145.       directly as glob($foo), which    is probably the    right way to
  2146.       have done it in the first place.)  Example:
  2147.  
  2148.           while (<*.c>) {
  2149.           chmod    0644, $_;
  2150.           }
  2151.  
  2152.       is equivalent    to
  2153.  
  2154.           open(FOO,    "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
  2155.           while (<FOO>) {
  2156.           chop;
  2157.           chmod    0644, $_;
  2158.           }
  2159.  
  2160.       In fact, it's    currently implemented that way.     (Which    means
  2161.       it will not work on filenames    with spaces in them unless you
  2162.       have _c_s_h(1) on your machine.)     Of course, the    shortest way
  2163.       to do    the above is:
  2164.  
  2165.           chmod 0644, <*.c>;
  2166.  
  2167.       Because globbing invokes a shell, it's often faster to call
  2168.       _r_e_a_d_d_i_r() yourself and do your own _g_r_e_p() on the filenames.
  2169.       Furthermore, due to its current implementation of using a
  2170.       shell, the _g_l_o_b() routine may    get "Arg list too long"    errors
  2171.       (unless you've installed _t_c_s_h(1L) as /_b_i_n/_c_s_h).
  2172.  
  2173.  
  2174.  
  2175.      Page 33                        (printed 10/23/98)
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  2183.  
  2184.  
  2185.  
  2186.       A glob evaluates its (embedded) argument only    when it    is
  2187.       starting a new list.    All values must    be read    before it will
  2188.       start    over.  In a list context this isn't important, because
  2189.       you automatically get    them all anyway.  In scalar context,
  2190.       however, the operator    returns    the next value each time it is
  2191.       called, or a undef value if you've just run out. As for
  2192.       filehandles an automatic defined is generated    when the glob
  2193.       occurs in the    test part of a while or    for - because legal
  2194.       glob returns (e.g. a file called _0) would otherwise
  2195.       terminate the    loop.  Again, undef is returned    only once.  So
  2196.       if you're expecting a    single value from a glob, it is    much
  2197.       better to say
  2198.  
  2199.           ($file) =    <blurch*>;
  2200.  
  2201.       than
  2202.  
  2203.           $file = <blurch*>;
  2204.  
  2205.       because the latter will alternate between returning a
  2206.       filename and returning FALSE.
  2207.  
  2208.       It you're trying to do variable interpolation, it's
  2209.       definitely better to use the _g_l_o_b() function,    because    the
  2210.       older    notation can cause people to become confused with the
  2211.       indirect filehandle notation.
  2212.  
  2213.           @files = glob("$dir/*.[ch]");
  2214.           @files = glob($files[$i]);
  2215.  
  2216.  
  2217.       CCCCoooonnnnssssttttaaaannnntttt FFFFoooollllddddiiiinnnngggg
  2218.  
  2219.       Like C, Perl does a certain amount of    expression evaluation
  2220.       at compile time, whenever it determines that all arguments
  2221.       to an    operator are static and    have no    side effects.  In
  2222.       particular, string concatenation happens at compile time
  2223.       between literals that    don't do variable substitution.
  2224.       Backslash interpretation also    happens    at compile time.  You
  2225.       can say
  2226.  
  2227.           'Now is the time for all'    . "\n" .
  2228.           'good    men to come to.'
  2229.  
  2230.       and this all reduces to one string internally.  Likewise, if
  2231.       you say
  2232.  
  2233.           foreach $file (@filenames) {
  2234.           if (-s $file > 5 + 100 * 2**16) {  }
  2235.           }
  2236.  
  2237.       the compiler will precompute the number that expression
  2238.  
  2239.  
  2240.  
  2241.      Page 34                        (printed 10/23/98)
  2242.  
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  2249.  
  2250.  
  2251.  
  2252.       represents so    that the interpreter won't have    to.
  2253.  
  2254.       BBBBiiiittttwwwwiiiisssseeee SSSSttttrrrriiiinnnngggg OOOOppppeeeerrrraaaattttoooorrrrssss
  2255.  
  2256.       Bitstrings of    any size may be    manipulated by the bitwise
  2257.       operators (~ | & ^).
  2258.  
  2259.       If the operands to a binary bitwise op are strings of
  2260.       different sizes, oooorrrr and xxxxoooorrrr ops will act as if the shorter
  2261.       operand had additional zero bits on the right, while the aaaannnndddd
  2262.       op will act as if the    longer operand were truncated to the
  2263.       length of the    shorter.
  2264.  
  2265.           #    ASCII-based examples
  2266.           print "j p \n" ^ " a h";          # prints "JAPH\n"
  2267.           print "JA" | "  ph\n";          # prints "japh\n"
  2268.           print "japh\nJunk" & '_____';      # prints "JAPH\n";
  2269.           print 'p N$' ^ " E<H\n";          # prints "Perl\n";
  2270.  
  2271.       If you are intending to manipulate bitstrings, you should be
  2272.       certain that you're supplying    bitstrings: If an operand is a
  2273.       number, that will imply a nnnnuuuummmmeeeerrrriiiicccc bitwise operation. You may
  2274.       explicitly show which    type of    operation you intend by    using
  2275.       "" or    0+, as in the examples below.
  2276.  
  2277.           $foo =  150  |  105 ;      # yields 255    (0x96 |    0x69 is    0xFF)
  2278.           $foo = '150' |  105 ;      # yields 255
  2279.           $foo =  150  | '105';      # yields 255
  2280.           $foo = '150' | '105';      # yields string '155'    (under ASCII)
  2281.  
  2282.           $baz = 0+$foo & 0+$bar;      # both ops explicitly    numeric
  2283.           $biz = "$foo" ^ "$bar";      # both ops explicitly    stringy
  2284.  
  2285.  
  2286.       IIIInnnntttteeeeggggeeeerrrr AAAArrrriiiitttthhhhmmmmeeeettttiiiicccc
  2287.  
  2288.       By default Perl assumes that it must do most of its
  2289.       arithmetic in    floating point.     But by    saying
  2290.  
  2291.           use integer;
  2292.  
  2293.       you may tell the compiler that it's okay to use integer
  2294.       operations from here to the end of the enclosing BLOCK.  An
  2295.       inner    BLOCK may countermand this by saying
  2296.  
  2297.           no integer;
  2298.  
  2299.       which    lasts until the    end of that BLOCK.
  2300.  
  2301.       The bitwise operators    ("&", "|", "^",    "~", "<<", and ">>")
  2302.       always produce integral results.  (But see also the section
  2303.       on _B_i_t_w_i_s_e _S_t_r_i_n_g _O_p_e_r_a_t_o_r_s.)     However, use integer still
  2304.  
  2305.  
  2306.  
  2307.      Page 35                        (printed 10/23/98)
  2308.  
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  2315.  
  2316.  
  2317.  
  2318.       has meaning for them.     By default, their results are
  2319.       interpreted as unsigned integers.  However, if use integer
  2320.       is in    effect,    their results are interpreted as signed
  2321.       integers.  For example, ~0 usually evaluates to a large
  2322.       integral value.  However, use    integer; ~0 is -1 on twos-
  2323.       complement machines.
  2324.  
  2325.       FFFFllllooooaaaattttiiiinnnngggg----ppppooooiiiinnnntttt AAAArrrriiiitttthhhhmmmmeeeettttiiiicccc
  2326.  
  2327.       While    use integer provides integer-only arithmetic, there is
  2328.       no similar ways to provide rounding or truncation at a
  2329.       certain number of decimal places.  For rounding to a certain
  2330.       number of digits, _s_p_r_i_n_t_f() or _p_r_i_n_t_f() is usually the
  2331.       easiest route.
  2332.  
  2333.       Floating-point numbers are only approximations to what a
  2334.       mathematician    would call real    numbers.  There    are infinitely
  2335.       more reals than floats, so some corners must be cut.    For
  2336.       example:
  2337.  
  2338.           printf "%.20g\n",    123456789123456789;
  2339.           #           produces    123456789123456784
  2340.  
  2341.       Testing for exact equality of    floating-point equality    or
  2342.       inequality is    not a good idea.  Here's a (relatively
  2343.       expensive) work-around to compare whether two    floating-point
  2344.       numbers are equal to a particular number of decimal places.
  2345.       See Knuth, volume II,    for a more robust treatment of this
  2346.       topic.
  2347.  
  2348.           sub fp_equal {
  2349.           my ($X, $Y, $POINTS) = @_;
  2350.           my ($tX, $tY);
  2351.           $tX =    sprintf("%.${POINTS}g",    $X);
  2352.           $tY =    sprintf("%.${POINTS}g",    $Y);
  2353.           return $tX eq    $tY;
  2354.           }
  2355.  
  2356.       The POSIX module (part of the    standard perl distribution)
  2357.       implements _c_e_i_l(), _f_l_o_o_r(), and a number of other
  2358.       mathematical and trigonometric functions.  The Math::Complex
  2359.       module (part of the standard perl distribution) defines a
  2360.       number of mathematical functions that    can also work on real
  2361.       numbers.  Math::Complex not as efficient as POSIX, but POSIX
  2362.       can't    work with complex numbers.
  2363.  
  2364.       Rounding in financial    applications can have serious
  2365.       implications,    and the    rounding method    used should be
  2366.       specified precisely.    In these cases,    it probably pays not
  2367.       to trust whichever system rounding is    being used by Perl,
  2368.       but to instead implement the rounding    function you need
  2369.       yourself.
  2370.  
  2371.  
  2372.  
  2373.      Page 36                        (printed 10/23/98)
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  2381.  
  2382.  
  2383.  
  2384.       BBBBiiiiggggggggeeeerrrr NNNNuuuummmmbbbbeeeerrrrssss
  2385.  
  2386.       The standard Math::BigInt and    Math::BigFloat modules provide
  2387.       variable precision arithmetic    and overloaded operators.  At
  2388.       the cost of some space and considerable speed, they avoid
  2389.       the normal pitfalls associated with limited-precision
  2390.       representations.
  2391.  
  2392.           use Math::BigInt;
  2393.           $x = Math::BigInt->new('123456789123456789');
  2394.           print $x * $x;
  2395.  
  2396.           #    prints +15241578780673678515622620750190521
  2397.  
  2398.  
  2399.  
  2400.  
  2401.  
  2402.  
  2403.  
  2404.  
  2405.  
  2406.  
  2407.  
  2408.  
  2409.  
  2410.  
  2411.  
  2412.  
  2413.  
  2414.  
  2415.  
  2416.  
  2417.  
  2418.  
  2419.  
  2420.  
  2421.  
  2422.  
  2423.  
  2424.  
  2425.  
  2426.  
  2427.  
  2428.  
  2429.  
  2430.  
  2431.  
  2432.  
  2433.  
  2434.  
  2435.  
  2436.  
  2437.  
  2438.  
  2439.      Page 37                        (printed 10/23/98)
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  2447.  
  2448.  
  2449.  
  2450.  
  2451.  
  2452.  
  2453.  
  2454.  
  2455.  
  2456.  
  2457.  
  2458.  
  2459.  
  2460.  
  2461.  
  2462.  
  2463.  
  2464.  
  2465.  
  2466.  
  2467.  
  2468.  
  2469.  
  2470.  
  2471.  
  2472.  
  2473.  
  2474.  
  2475.  
  2476.  
  2477.  
  2478.  
  2479.  
  2480.  
  2481.  
  2482.  
  2483.  
  2484.  
  2485.  
  2486.  
  2487.  
  2488.  
  2489.  
  2490.  
  2491.  
  2492.  
  2493.  
  2494.  
  2495.  
  2496.  
  2497.  
  2498.  
  2499.  
  2500.  
  2501.  
  2502.      Page 38                        (printed 10/23/98)
  2503.  
  2504.  
  2505.  
  2506.  
  2507.  
  2508.  
  2509.